package com.example.autopia.utilities

import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class TimeManager {
    fun unixTimeToDateTime(unixTime: Long): String {
        val date = Date(unixTime * 1000L)
        val pattern = "dd-MM-yyyy HH:mm:ss" // 16-10-2023 12:02:21
        val sdf = SimpleDateFormat(pattern, Locale.getDefault())
        return sdf.format(date)
    }

    fun unixTimeToDateTimeComment(unixTime: Int): String {
        val date = Date(unixTime * 1000L)
        val today = Date() // Текущая дата и время
        val pattern: String = if (isSameDay(date, today)) {
            "сегодня в HH:mm" // сегодня в 19:31
        } else {
            val sdf = SimpleDateFormat("yyyy", Locale("ru", "RU"))
            val currentYear = sdf.format(today)
            val year = sdf.format(date)

            if (year == currentYear) {
                "d MMMM HH:mm" // 26 сентября 19:31
            } else {
                "d MMM yyyy HH:mm" // 26 сентября 2021 19:31
            }
        }
        val sdf = SimpleDateFormat(pattern, Locale.getDefault())
        return sdf.format(date)
    }

    private fun isSameDay(date1: Date, date2: Date): Boolean {
        val cal1 = java.util.Calendar.getInstance()
        cal1.time = date1
        val cal2 = java.util.Calendar.getInstance()
        cal2.time = date2
        return cal1.get(java.util.Calendar.YEAR) == cal2.get(java.util.Calendar.YEAR) &&
                cal1.get(java.util.Calendar.DAY_OF_YEAR) == cal2.get(java.util.Calendar.DAY_OF_YEAR)
    }

    fun secondsToTime(seconds: Int): String {
        if (seconds < 0) return "0 секунд"

        val days = seconds / (60 * 60 * 24)
        val hours = (seconds % (60 * 60 * 24)) / (60 * 60)
        val minutes = (seconds % (60 * 60)) / 60
        val remainingSeconds = seconds % 60

        val formattedTime = buildString {
            if (days > 0) {
                append("$days дней")
            }
            if (hours > 0) {
                if (isNotEmpty()) append(", ")
                append("$hours часов")
            }
            if (minutes > 0) {
                if (isNotEmpty()) append(", ")
                append("$minutes минут")
            }
            if (remainingSeconds > 0 || isEmpty()) {
                if (isNotEmpty()) append(", ")
                append("$remainingSeconds секунд")
            }
        }

        return formattedTime
    }
    fun getCurrentUnixTime(): Int {
        return (System.currentTimeMillis() / 1000).toInt()
    }
    fun getTimeWithFiveMins(): Int {
        return ((System.currentTimeMillis() / 1000).toInt()+30)
    }
    fun formatMilliseconds(millis: Long): String {
        val minutes = (millis / 1000 / 60).toInt()
        val seconds = ((millis / 1000) % 60).toInt()
        return String.format("%d:%02d", minutes, seconds)
    }
    fun formatDelta(seconds: Int): String {
        val hours = (seconds / 60 / 60).toInt()
        val minutes = ((seconds % 60)).toInt()
        return String.format("%d:%02d", hours, minutes)
    }
    fun formatSeconds(seconds: Long): String {
        val hours = (seconds / 3600).toInt()
        val minutes = ((seconds % 3600) / 60).toInt()

        val hoursString = if (hours > 0) "$hours ч." else ""
        val minutesString = if (minutes > 0) "$minutes мин." else ""

        val separator = if (hours > 0 && minutes > 0) " " else ""

        return "$hoursString$separator$minutesString"
    }
}